import pygame
import sys

from pygame.sprite import DirtySprite
from pygame.sprite import LayeredDirty

from state_manager import *
from settings_manager import *
from button import *
from score_line import *
from events import *

import math

path = "data/textures/"

class HighscoreScene:
	refresh = False

	def __init__(self, screen):
		self.screen = screen

	def create(self):
		self.group = LayeredDirty()

		self.background = pygame.image.load(path + str(SettingsManager.settings.resolution[0]) + "_highscore_background.jpg")

		self.bBack = Button(path + "back1.png", path + "back2.png", path + "back3.png")	#back to MainMenu

		self.scoreImage = pygame.image.load(path + str(SettingsManager.settings.resolution[0]) + "_scoreline.png")
		self.createScoreBoard()

		#Set buttons in place
		width, height = self.screen.get_rect().width, self.screen.get_rect().height
		bWidth, bHeight = self.bBack.rect.width, self.bBack.rect.height
		space = 30

		posX = width - bWidth - space
		posY = height - bHeight - space
		self.bBack.rect.x = posX
		self.bBack.rect.y = posY

		#Merge all sprites in one group
		self.group.add(self.bBack)


	def createScoreBoard(self):
		scores = []
		with open("data/score/" + "scores", "r") as f:
			for line in f:
				aux = line.split()
				if len(aux) == 0:
					continue
				score = int(aux[0])
				name = ""
				for i in aux[1 :]:
					name = name + " " + i

				scores.append((score, name))

		width, height = self.screen.get_rect().width, self.screen.get_rect().height
		bWidth, bHeight = self.scoreImage.get_rect().width, self.scoreImage.get_rect().height
		space = 0

		if SettingsManager.settings.resolution[0] == SettingsConst.Minimum[0]:
			posX,posY = 154, 145
		elif SettingsManager.settings.resolution[0] == SettingsConst.Medium[0]:
			posX,posY = 195,182
		elif SettingsManager.settings.resolution[0] == SettingsConst.Maximum[0]:
			posX,posY = 200,200


		position = 0
		for score, name in sorted(scores, reverse = True):
			position = position + 1
			if position > 10:
				break
				
			self.group.add(ScoreLine(self.scoreImage,
			 name, str(score),
			 int(30.0 / SettingsConst.Medium[0] * SettingsManager.settings.resolution[0]), 
			 (posX, posY)))

			posY += bHeight + space

		for i in xrange(position, 11):	#Fill the table
			self.group.add(ScoreLine(self.scoreImage, 
				"", "", 
				int(30.0 / SettingsConst.Medium[0] * SettingsManager.settings.resolution[0]), 
				(posX, posY)))

			posY += bHeight + space

	def update(self):
		if StateManager.state.window != StateConst.Highscore:
			return None #Escape settings updating

		self.manageButtonsClicks()

		self.drawHighscore()

		if HighscoreScene.refresh:
			HighscoreScene.refresh = False
			self.create()

	def manageButtonsClicks(self):
		if self.bBack.isClicked():
			StateManager.changeWindow(StateConst.MainMenu)
			StateManager.changeSound(StateConst.MainMenuSound)
	
	def drawHighscore(self):
		self.group.clear(self.screen, self.background)
		update_area = self.group.draw(self.screen)
		pygame.display.update(update_area)
		self.group.update()